<?php
/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2018/3/3
 * Time: 下午6:47
 */

namespace App\Model;


use App\Model\Model;
use App\Bean\Switchor as Bean;
use EasySwoole\Core\Utility\Random;

class Switchor extends Model
{
    protected $table = 'data_switch';


    public function delete(Bean $bean)
    {
        return $this->dbConnector()->where('id',$bean->getId())->delete($this->table);
    }

    public function update(Bean $bean,array $data)
    {
        return $this->dbConnector()->where('id',$bean->getId())->update($this->table,$data);
    }

    public function find($hard_id) {
        $info = $this->dbConnector()->where('device_id', $hard_id)
            ->orderBy('time', 'desc')
            ->getOne($this->table);

        if(!empty($info)){
            return new Bean($info);
        }else{
            return null;
        }
    }

    public function select($hard_id, $start, $end) {
        $info = $this->dbConnector()->where('device_id', $hard_id)
            ->where('time', [$start, $end], 'BETWEEN')
            ->get($this->table);

        if (!$info) {
            return null;
        } else {
            foreach ($info as $item){
                $data[] = new Bean($item);
            }
            return $data;
        }
    }
}